package com.ruicar.afs.cloud.channel.common.mq.sender.impl;

import com.ruicar.afs.cloud.channel.affiliation.entity.ChannelAffiliatedUnitsRelTemp;
import com.ruicar.afs.cloud.channel.affiliation.service.ChannelAffiliatedUnitsRelTempService;
import com.ruicar.afs.cloud.common.modules.dto.mq.affiliation.AffiliationRelDto;
import com.ruicar.afs.cloud.common.modules.dto.mq.affiliation.AffiliationRelStatusDto;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * 进件放款提交案件审批实现类
 *
 * @author panhj
 */
@Service
@AllArgsConstructor
public class CaseAffiliationRelStatusSenderImpl {
    /**
     * 挂靠关联合作商service
     */
    private final ChannelAffiliatedUnitsRelTempService channelAffiliatedUnitsRelTempService;


    /**
     * 返回
     *
     * @param id
     * @return
     */
    public AfsTransEntity<AffiliationRelStatusDto> returnAffiliationRelInfo(String id) {
        AfsTransEntity<AffiliationRelStatusDto> transEntity = new AfsTransEntity<>();
        AffiliationRelStatusDto affiliationRelDto = new AffiliationRelStatusDto();
        ArrayList<AffiliationRelDto> affiliationRelDtoArrayList = this.affiliationRelDtoArrayList(id);
        affiliationRelDto.setAffiliationRelDtoArrayList(affiliationRelDtoArrayList);
        transEntity.setTransCode(MqTransCode.AFS_CHANNEL_CHANNEL_CASE_CTM_UPDATE_REL);
        transEntity.setData(affiliationRelDto);
        return transEntity;
    }

    /**
     * 保存挂靠关联的合作商信息
     */
    public ArrayList<AffiliationRelDto> affiliationRelDtoArrayList(String id) {
        ArrayList<AffiliationRelDto> affiliationRelDtoArrayList = new ArrayList<>();
        String[] idArray = id.split(",");
        for (int j = 0; j < idArray.length; j++) {
            Long ids = Long.valueOf(idArray[j]);
            ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = this.channelAffiliatedUnitsRelTempService.getById(ids);
            AffiliationRelDto affiliationRelDto = new AffiliationRelDto();
            affiliationRelDto.setArriliatedId(channelAffiliatedUnitsRelTemp.getArriliatedId());
            affiliationRelDto.setChannelId(channelAffiliatedUnitsRelTemp.getChannelId());
            affiliationRelDto.setCreateBy(channelAffiliatedUnitsRelTemp.getCreateBy());
            affiliationRelDto.setCreateTime(channelAffiliatedUnitsRelTemp.getCreateTime());
            affiliationRelDto.setDelFlag(channelAffiliatedUnitsRelTemp.getDelFlag());
            affiliationRelDto.setId(channelAffiliatedUnitsRelTemp.getId());
            affiliationRelDto.setStatus(channelAffiliatedUnitsRelTemp.getStatus());
            affiliationRelDto.setUpdateBy(channelAffiliatedUnitsRelTemp.getUpdateBy());
            affiliationRelDto.setUpdateTime(channelAffiliatedUnitsRelTemp.getUpdateTime());
            affiliationRelDto.setChannelTermValidity(channelAffiliatedUnitsRelTemp.getChannelTermValidity());
            affiliationRelDtoArrayList.add(affiliationRelDto);

        }
        return affiliationRelDtoArrayList;
    }
}
